package org.viettan.android.main.model;

import org.viettan.android.main.io.UrlUtility;

import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;

import com.google.common.base.Preconditions;

/**
 * A single RCTM episode.
 */
@SuppressWarnings("serial")
public class RadioEpisode implements Entity, Parcelable {

    // marking everything as transient, as we are serializing them ourselves
    private int id;
    private String title;
    private String subtitle;
    private String author;
    private String mp3Url;

    /**
     * Constructs a new {@code RadioEpisode}.
     */
    public RadioEpisode() {
    }

    private RadioEpisode(Parcel in) {
	readFromParcel(in);
    }

    /**
     * Sets the ID of the episode.
     *
     * @param id
     * @return
     */
    public RadioEpisode setId(int id) {
	Preconditions.checkArgument(id > 0, "Id must be positive");

	this.id = id;
	return this;
    }

    /**
     * Returns the ID of the episode.
     */
    public int getId() {
	return id;
    }

    /**
     * Sets the title of the episode.
     *
     * @param title
     * @return
     */
    public RadioEpisode setTitle(String title) {
	this.title = title;
	return this;
    }

    /**
     * Returns the title of the episode.
     */
    public String getTitle() {
	return title;
    }

    /**
     * Sets the subtitle of the episode.
     *
     * @param subtitle
     * @return
     */
    public RadioEpisode setSubtitle(String subtitle) {
	this.subtitle = subtitle;
	return this;
    }

    /**
     * Returns the title of the episode.
     */
    public String getSubtitle() {
	return subtitle;
    }

    /**
     * Sets the author of the episode.
     *
     * @param author
     */
    public RadioEpisode setAuthor(String author) {
	this.author = author;
	return this;
    }

    /**
     * Returns the author of the episode.
     */
    public String getAuthor() {
	return author;
    }

    /**
     * Sets the URL to the actual audio file of the episode.
     *
     * @param mp3Url
     * @return
     */
    public RadioEpisode setMp3Url(Uri mp3Url) {
	// purposely using a String, since Uri is not Serializable
	this.mp3Url = mp3Url.toString();
	return this;
    }

    /**
     * Returns the URL to the actual audio file of the episode.
     */
    public Uri getMp3Url() {
	return UrlUtility.parseUrl(mp3Url);
    }

    public static final Parcelable.Creator<RadioEpisode> CREATOR = new Parcelable.Creator<RadioEpisode>() {
	public RadioEpisode createFromParcel(Parcel in) {
	    return new RadioEpisode(in);
	}

	public RadioEpisode[] newArray(int size) {
	    return new RadioEpisode[size];
	}
    };

    public int describeContents() {
	return 0;
    }

    private void readFromParcel(Parcel in) {
	id = in.readInt();
	title = in.readString();
	subtitle = in.readString();
	author = in.readString();
	mp3Url = in.readString();
    }

    public void writeToParcel(Parcel dest, int flags) {
	dest.writeInt(id);
	dest.writeString(title);
	dest.writeString(subtitle);
	dest.writeString(author);
	dest.writeString(mp3Url.toString());
    }
}
